package com.api.mc
{
	import api.dataStruct.GameApiInfo;
	
	import com.api.StaticData;
	import api.dataStruct.FaceDataObj;
	import com.api.vo.SendChatObj;
	import com.game.btnClass.AddMouseEvent;
	import com.game.btnClass.BtnClass_goto;
	import com.game.display.AutoGCMovieClip;
	import com.game.utils.LoadSwf;
	
	import fl.controls.TextArea;
	import fl.controls.TextInput;
	
	import flash.display.DisplayObject;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	
	public class ChatMc extends BasicMc
	{
		//swf素材
		public var chatList:TextArea;
		public var input:TextInput;
		public var faceIcon:MovieClip;
		public var sendMsg:MovieClip;
		public var bgMc:MovieClip;
		
		
		
		
		//显示对象
		private var _myHeadMc:UserHeadMc
		private var _otherHeadMc:UserHeadMc
		private var _myDisFace:DisFace
		private var _otherDisFace:DisFace
		private var _faceList:FaceList;
		
		
		//数据
		private var _faceEvent:AddMouseEvent;
		private var _headAreaWidth:uint=210;
		private var _otherAreaWidth:uint
		private var _sendFun:Function;
		private var _loadSwf:LoadSwf=new LoadSwf();
		
		
		public function ChatMc()
		{
		}
		public function initChat(sendFun:Function=null):void{
			_sendFun=sendFun;
			init();
		}
		private function init():void{
			initUI();
			initBtn();
		}
		private function initUI():void{
			
			_otherAreaWidth=StaticData.gameW-_headAreaWidth;
			bgMc.width=StaticData.gameW;
			chatList.x=faceIcon.x=_headAreaWidth;
			input.x=faceIcon.x+faceIcon.width+5;
			
			chatList.width=_otherAreaWidth-10;
			input.width=_otherAreaWidth-20-sendMsg.width-faceIcon.width;
			sendMsg.x=input.x+input.width+5;
			
			input.addEventListener(FocusEvent.FOCUS_IN,inputFocusIn);
		}
		private function inputFocusIn(e:FocusEvent):void{
			
			input.removeEventListener(FocusEvent.FOCUS_IN,inputFocusIn);
			input.addEventListener(FocusEvent.FOCUS_OUT,inputFocusOut);
			
			input.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
		}
		private function inputFocusOut(e:FocusEvent):void{
			input.addEventListener(FocusEvent.FOCUS_IN,inputFocusIn);
			input.removeEventListener(KeyboardEvent.KEY_DOWN,keyDown);
			input.removeEventListener(FocusEvent.FOCUS_OUT,inputFocusOut);
		}
		private function keyDown(e:KeyboardEvent):void{
			if(e.keyCode==13){
				sendMsgClick();
			}
		}
		private function initBtn():void{
			addMouseEvent(new BtnClass_goto(faceIcon,showFaceList));
			addMouseEvent(new BtnClass_goto(sendMsg,sendMsgClick));
		}
		private function sendMsgClick():void{
			var _str:String=input.text;
			if(_str!=""){
				addChatList(_str);
				input.text="";
				_sendFun(new SendChatObj(_str));
				
			}
		}
		private function showFaceList():void{
			if(_faceList==null){
				_faceList=new FaceList(sendFace,delFaceList);
				_faceList.x=faceIcon.x;
				_faceList.y=faceIcon.y-_faceList.height-5;
				addChild(_faceList);
			}else{
				_faceList.visible=true;
			}
		}
		private function sendFace(faceDataObj:FaceDataObj):void{
			showFace(faceDataObj.fName);
			addChatList("",faceDataObj.desc);
			_sendFun(new SendChatObj(faceDataObj,1));
		}
		
		/**
		 * 添加用户头像
		 * @param url  表情地址
		 * @param isMe 是否为自己
		 * 
		 **/
		public function addUserHead(gameUserInfo:GameApiInfo,isMe:Boolean=true):void{
			
			if(isMe){
				if(_myHeadMc==null){
					_myHeadMc=new UserHeadMc();
					_myDisFace=new DisFace();
					_myHeadMc.headPic.gotoAndStop(Number(gameUserInfo.headPic));
					_myHeadMc.name_txt.text=gameUserInfo.nick;
					_myDisFace.x=_myHeadMc.x=30
					_myDisFace.y=_myHeadMc.y=20
					this.addChild(_myHeadMc);
					this.addChild(_myDisFace);
				}
			}else{
				if(_otherHeadMc==null){
					_otherHeadMc=new UserHeadMc();
					_otherDisFace=new DisFace();
					_otherHeadMc.name_txt.text=gameUserInfo.nick;
					_otherHeadMc.headPic.gotoAndStop(Number(gameUserInfo.headPic));
					_otherDisFace.x=_otherHeadMc.x=130;
					_otherDisFace.y=_otherHeadMc.y=20;
					this.addChild(_otherHeadMc);
					this.addChild(_otherDisFace);
				}
			}
			if(_myHeadMc!=null){
				//this.setChildIndex(_myDisFace,this.numChildren-1)
			}
			if(_myHeadMc!=null){
				//this.setChildIndex(_otherDisFace,this.numChildren-1)
			}
			
		}
		/**
		 * 删除用户头像
		 * 
		 **/
		public function delUserHead():void{
			if(_otherHeadMc!=null){
				
				this.removeChild(_otherHeadMc)
			}
			if(_otherDisFace!=null){
				
				this.removeChild(_otherDisFace)
			}
			_otherHeadMc=null;
			_otherDisFace=null
		}
		public function gameClew(str:String):void{
			chatList.htmlText+="<font size='12'>"+str+"\n";
			chatList.verticalScrollPosition=chatList.maxVerticalScrollPosition;
		}
		/**
		 * 添加聊天记录
		 * @param str  聊天内容
		 * @param faseName 表情名称
		 * @param userName 用户名称 =null为自己
		 * 
		 **/
		public function addChatList(str:String,faseName:String=null,userName:String=null):void{
			var _str:String="<font size='12'>";
			if (userName==null) {
				_str += "<font color='#008040'>我：</font>";
			} else {
				_str += "<font color='#0000FF'>"+userName+"：</font>";
			}
			if(faseName!=null){
				_str+="发送表情 <font color='#FF0000'>"+faseName+"</font>";
			}else{
				_str+=str;
			}
			chatList.htmlText+=_str+"\n";
			chatList.verticalScrollPosition=chatList.maxVerticalScrollPosition
		}
		/**
		 * 显示表情
		 * @param url  表情地址
		 * @param isMe 是否为自己
		 * 
		 **/
		public function showFace(url:String,isMe:Boolean=true):void{
			_loadSwf.setLoad("resource/face/"+url,function faceLoadCom(loaderInfo:LoaderInfo):void{
				
				if(isMe){
					if(_myDisFace==null){
						return;
					}
					_myDisFace.addFace(loaderInfo);	
				}else{
					if(_otherDisFace==null){
						return;
					}
					_otherDisFace.addFace(loaderInfo);
				}
			})
		}
		
		private function delFaceList():void{
			removeChild(_faceList);
			_faceList=null;
		}
		public function gc():void{
			delMouseEvent();
		}
		
	}
}